package com.sh.caryr.erp.controller.stock;


import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sh.caryr.erp.entity.PageResultMessage;
import com.sh.caryr.erp.entity.ResultMessage;
import com.sh.caryr.erp.exception.ResultCode;
import com.sh.caryr.erp.entity.stock.SkuRecord;
import com.sh.caryr.erp.entity.user.User;
import com.sh.caryr.erp.exception.RestServiceException;
import com.sh.caryr.erp.service.ISkuRecordService;
import com.sh.caryr.erp.util.ERPConstants;
import com.sh.caryr.erp.util.MyBatisUtil;
import com.sh.caryr.erp.util.UtilityTool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * skuRecord流水表 前端控制器
 * </p>
 *
 * @author milkeeper
 * @since 2017-12-01
 */
@RestController
@RequestMapping("/api/skuRecord")
@SessionAttributes("user")
public class SkuRecordController {
    @Autowired
    private ISkuRecordService skuRecordService;

    @ApiOperation(value = "获取skuRecord列表信息", notes = "获取skuRecord列表信息")
    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<SkuRecord>> findSkuRecords(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String queryField,
            @RequestParam(required = false) String queryValue,
            @RequestParam(required = false) Boolean isAccurate,
            @RequestParam(defaultValue = "id") String orderBy,
            @RequestParam(defaultValue = "false") Boolean isAsc,
            @ModelAttribute("user") User operator) {

        Wrapper wrapper = MyBatisUtil.getIsActiveWrapper(queryField, queryValue,orderBy,isAsc);
        MyBatisUtil.addAuthFilter(wrapper, operator, ERPConstants.AUTH_TARGET_USERROLE, false, false);
        List<SkuRecord> skuRecords = skuRecordService.findSkuRecordDynamic(current, size, wrapper, orderBy);
        return new PageResultMessage<>(skuRecords, skuRecordService.selectCount(wrapper));
    }

    @ApiOperation(value = "获取单个skuRecord信息", notes = "获取单个skuRecord信息")
    @ApiImplicitParam(name = "id", value = "skuRecordId", required = true, dataType = "long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<SkuRecord> findSkuRecordById(@PathVariable long id) {
        return new ResultMessage<>(skuRecordService.selectById(id));
    }

    @ApiOperation(value = "创建skuRecord", notes = "根据skuRecord对象创建skuRecord")
    @ApiImplicitParam(name = "skuRecord", value = "skuRecord详细实体", required = true, dataType = "SkuRecord")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<SkuRecord> insertSkuRecord(@RequestBody SkuRecord skuRecord) {
        if (skuRecordService.insert(skuRecord)) {
            return new ResultMessage<SkuRecord>(skuRecord);
        } else {
            throw new RestServiceException(ResultCode.CREATE_FAILED);
        }
    }

    @ApiOperation(value = "更新skuRecord", notes = "根据skuRecord对象更新skuRecord")
    @ApiImplicitParam(name = "skuRecord", value = "skuRecord详细实体", required = true, dataType = "SkuRecord")
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<SkuRecord> updateSkuRecordById(@RequestBody SkuRecord skuRecord) {
        if (skuRecordService.updateById(skuRecord)) {
            return new ResultMessage<>(skuRecord);
        } else {
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }
    }

    @ApiOperation(value = "批量删除skuRecord", notes = "根据url的id来指定删除skuRecord")
    @ApiImplicitParam(name = "ids", value = "skuRecordIds", required = true, dataType = "String")
    @Transactional
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage deleteSkuRecords(@PathVariable String ids) {
        List<Long> list = UtilityTool.convertStringArray2LongList(ids.split(","));
        try {
            UtilityTool.softDelete(SkuRecord.class, list, skuRecordService);
            return new ResultMessage();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RestServiceException(ResultCode.DELETE_NOT_FOUND);
        }
    }
}
